package com.example.mediademo;

import android.graphics.SurfaceTexture;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Environment;
import android.view.Surface;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.mediademo.decode.AudioDecoder;
import com.example.mediademo.decode.VideoDecoder;
import com.example.mediademo.opengl.DefGlSurfaceView;
import com.example.mediademo.opengl.IDrawder;
import com.example.mediademo.opengl.SimpleRender;
import com.example.mediademo.opengl.SurfaceTextureCallback;
import com.example.mediademo.opengl.VideoDrawer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MutilOpenGLPlayerActivity extends AppCompatActivity {

    private String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mv_test4.mp4";
    private String path2 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mvtest2.mp4";

    private DefGlSurfaceView glSurfaceView;
    ExecutorService threadPool = new ThreadPoolExecutor(4,20,0, TimeUnit.SECONDS,new LinkedBlockingQueue<>());

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opengl_player);
        glSurfaceView = findViewById(R.id.gl_surface);
        initRender();
    }

    private void initRender() {
        // 全屏Drawer
        IDrawder fullDrawer = new VideoDrawer(false);
        fullDrawer.setVideoSize(640,400);
        fullDrawer.getSurfaceTexture(surfaceTexture -> initPlayer(path,new Surface(surfaceTexture)));
        // 画中画Drawer
//        IDrawder smallDrawer = new VideoDrawer(false);
//        smallDrawer.setVideoSize(544,960);
//        smallDrawer.setAlpha(0.5f);
//        smallDrawer.getSurfaceTexture(surfaceTexture -> initPlayer(path2,new Surface(surfaceTexture)));

        SimpleRender simpleRender = new SimpleRender(fullDrawer);
//        simpleRender.addDrawer(smallDrawer);
        glSurfaceView.addDrawer((VideoDrawer) fullDrawer);
        glSurfaceView.setEGLContextClientVersion(2);
        glSurfaceView.setRenderer(simpleRender);
    }

    private void initPlayer(String pathStr,Surface surface) {
        VideoDecoder videeoDecoder = new VideoDecoder(pathStr,null,surface);
        threadPool.execute(videeoDecoder);

        AudioDecoder audioDecoder = new AudioDecoder(pathStr);
        threadPool.execute(audioDecoder);

        videeoDecoder.resume();
        audioDecoder.resume();
    }
}
